using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace BizTalkCrossReferenceManager.UI.Generation
{
    public partial class CSharpCodeGenerationForm : Form
    {
        public CSharpCodeGenerationForm()
        {
            InitializeComponent();
        }

        private void CSharpCodeGenerationForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'crossReferenceDataDataSet.xref_IDXRef' table. You can move, or remove it, as needed.
            this.xref_IDXRefTableAdapter.Fill(this.crossReferenceDataDataSet.xref_IDXRef);

        }

        private void GenerateButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(ReferenceTypeComboBox.Text)
                || string.IsNullOrEmpty(NamespaceTextBox.Text)
                || string.IsNullOrEmpty(ClassNameTextBox.Text))
            {
                MessageBox.Show("Please provide information for generation");
                return;
            }

            DialogResult dr = saveFileDialog1.ShowDialog();
            if (dr != DialogResult.OK)
                return;

            string outputPath = saveFileDialog1.FileName;            
            Generators.CSharpFileBuilder builder = new BizTalkCrossReferenceManager.UI.Generation.Generators.CSharpFileBuilder(ReferenceTypeComboBox.Text, NamespaceTextBox.Text, ClassNameTextBox.Text);
            string code = builder.Generate();
            Trace.WriteLine(code);
            using(FileStream fs = new FileStream(outputPath, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(fs))
                {
                    writer.Write(code);
                    writer.Flush();
                }
            }
        }
    }
}